<?php
require_once("inc.admin.php");

$this_title="$vars[admin_title] &raquo; ".__("Member Activation");
$page_title=__("Member Activation");

if(!$pv["task"]["Activation"]){
	$errmsg="<h2>$page_title</h2>".format_err("You do not have the privilege to access member activation.");

	print format_admin_page($errmsg, $this_title);
	exit();
}

$r_payment_channel=array('ewallet','eswallet','c');
$r_payment_channel_db=array('ewallet'=>'e','eswallet'=>'s','c'=>'c');
$r_payment_channel_d=array('ewallet'=>__($vars['ewallet_title']), 'eswallet'=>__($vars['eswallet_title']), 'c'=>__("Cash"));
$r_package=$vars['r_package'];
$r_package_d=$vars['r_package_detail'];

$td_width=180;
$datetime=ndate($vars["system_date_format"]);

//#####AJAX CALL#####
if($_GET["aj"] && $_GET["check_uid"]){
	if(!$r_ref=get_user_detail_by_id($post_d['uid'])){
		$ajerrmsg="Member ID '$post_s[uid]' could not be found.";
	}elseif($r_ref['rank_a']>=max($vars['rank_index_a'])){
		$ajerrmsg="Member ID '$post_s[uid] had already activated the top ranking and cannot be upgraded anymore.";
	}else{
		$ajmsg="Member ID: $post_s[uid], Name: $r_ref[name], Rank: ".($r_ref['rank_a']>0? $vars['rank_index_a_d'][$r_ref['rank_a']] : "None").", $vars[ewallet_title]: ".number_format($r_ref['ewallet'], 2);
	}
	$xml=array("status"=>($ajerrmsg? "2" : "1"), "msg"=>$ajmsg.$ajerrmsg);
	print format_xml($xml);
	exit();
}
//#####END AJAX CALL#####

//#####ACTIVATION POST#####
if($_POST["__req"]){
	$chk_arr="_remark";
	$chk_arrr=explode(",", $chk_arr);
	foreach($post_s as $f=>$v){
		if(in_array($f, $chk_arrr)){
			$data[$f]=$v;
		}
	}
	$errmsg=verify_form_data("purchase", $data);

	if(!$errmsg){
		if(!$post_s['uid'] || !is_numeric($post_s['uid']) || $post_s['uid'] <=0){
			$errmsg.="Please provide a valid Member ID.<br />\n";
		}elseif(!$r_user=get_user_detail_by_id($post_d['uid'])){
			$errmsg="Member ID '$post_s[uid]' could not be found.<br />\n";
		}elseif($r_user['rank_a']>=max($vars['rank_index_a'])){
			$errmsg="Member ID '$post_s[uid] had already activated the top ranking and cannot be upgraded anymore.<br />\n";
		}elseif($vars['package'][$post_s['package']]['rank'] <= $r_user['rank_a']){
			$errmsg="Member ID '$post_s[uid] is of ranking {$vars['rank_index_a_d'][$r_user['rank_a']]}, you must choose a higher package.<br />\n";
		}else{
			$r_ref = get_user_detail_by_id($r_user['ref']);
			if($r_ref && $r_ref['activated'] != 'y'){
				$errmsg.=replace_tag(__("The Sponsor of this member ID '<%uid%>' is not activated."), array("<%uid%>"=>$post_h['uid']))."<br />\n";
			}
		}
	}
	$purchase_descr = '';
	$refno = $post_s['package'];
	$qty = 1;
	$cost = $vars['package'][$refno]['price'] - ($r_user['rank_a']>0? $vars['rank_index_a_price'][$r_user['rank_a']] : 0);
	$total_bv = $total_dp = $total_rp = $vars['package'][$refno]['bv'] - ($r_user['rank_a']>0? $vars['rank_index_a_bv'][$r_user['rank_a']] : 0);
	$purchase_descr.=($purchase_descr? ", " : "").$r_package_d[$refno]." x ".$qty;
	$purchased_prods[$refno] = $qty;
	if(!count($purchased_prods)){
		$errmsg.=__("Please select a activation package to purchase.")."<br />\n";
	}
	if(!$errmsg){
		if(in_array($post_s['payment_channel'], array('ewallet','eswallet')) && $r_user[$post_s['payment_channel']]<$cost){
			$errmsg.=replace_tag(__("The member with ID '$post_s[uid]' ($r_user[name]) does not have sufficient <%title%> to purchase the products."), array("<%title%>"=>__($vars[$post_s['payment_channel'].'_title'])))."<br />\n".replace_tag(__("<%title%> required: <%x%>"), array("<%title%>"=>__($vars[$post_s['payment_channel'].'_title']), "<%x%>"=>number_format($cost, 2)))."<br />\n".replace_tag(__("<%title%> available: <%x%>"), array("<%title%>"=>__($vars[$post_s['payment_channel'].'_title']), "<%x%>"=>number_format($r_user[$post_s['payment_channel']], 2)))."<br />\n";
		}
	}

	if(!$errmsg){
		if(in_array($post_s['payment_channel'], array('ewallet','eswallet'))){
			if(!mysql_query($sql="update $db->users set $post_s[payment_channel]=$post_s[payment_channel]-$cost where id='$post_d[uid]' limit 1")){
				$errmsg.="There is some error while purchasing for the member: error deducting the member ".$r_payment_channel_d[$post_s['payment_channel']]." account.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
			}
		}
		if(!$errmsg){
			//activate member
			$rank_a = $vars['package'][$refno]['rank'];
			if(!mysql_query($sql="update $db->users set rank_a='$rank_a'".($r_user['activated']!='y'? ", activated='y', activated_date='$datetime'" : "")." where id='$post_d[uid]' limit 1")){
				$errmsg.="Error activating the member, please manually execute the SQL below to activate this member:<br />\n<br />\n$sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
			}
			$purchase_descr2="Purchase for $purchase_descr";
			//insert ewallet record
			if(in_array($post_s['payment_channel'], array('ewallet','eswallet'))){
				if(!mysql_query($sql="insert into {$vars["dbi"]["prefix"]}member_$post_s[payment_channel]_record (uid, type, amount, descr, cdate) values ('$post_d[uid]', 'debit', '$cost', '".AddSlashes("Debited $vars[currency]".number_format($cost, 2)." for $purchase_descr2")."', '$datetime')")){
					$errmsg.="Error creating the member ".$vars[$post_s['payment_channel'].'_title']." record. Please manually execute the SQL below to record the transaction:<br />\n<br />\n$sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
				}else{
					$new_eid = mysql_insert_id();
					$last_bal = @mysql_result(mysql_query("select bal from {$vars["dbi"]["prefix"]}member_$post_s[payment_channel]_record where uid='$post_d[uid]' and id!=$new_eid order by id desc limit 1"), 0);
					if(!$last_bal){
						$last_bal = 0;
					}
					$last_bal -= $cost;
					@mysql_query("update {$vars["dbi"]["prefix"]}member_$post_s[payment_channel]_record set bal='$last_bal' where id='$new_eid' limit 1");
				}
			}
			//insert sales record
			$first_sale = true;
			$this_rank=$vars['package'][$refno]['rank'];
			$payment_channel = $r_payment_channel_db[$post_s['payment_channel']];
			if(!mysql_query($sql="insert into $db->purchase (creator, cid, uid, member_sale, stockist_sale, amount, total_bv, total_dp, total_rp, payment_type, collected, first_sale, rank_a, status, remark, cdate, edate) values 
			('a', '$aid', '$post_d[uid]', 'y', 'n', '$cost', '$total_bv', '$total_dp', '$total_rp', '$payment_channel', 'n', 'y', '$this_rank', 'confirmed', '$post_d[_remark]', '$datetime', '$datetime')")){
				$errmsg.="Error creating the member sales record. Please manually execute the SQL below to record the transaction:<br />\n<br />\n$sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
				$pid="0";
			}else{
				$pid=mysql_insert_id();
				$sale_refno = $refno = "MS" . str_pad($pid, $vars['refno_min_digit'], '0', STR_PAD_LEFT);
				if(!mysql_query($sql="update $db->purchase set refno='$refno' where id='$pid' limit 1")){
					$errmsg.=__("Error recording the sale reference number. Please manually record the sale reference number by executing SQL.")."<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
				}
				//insert sales detail
				foreach($purchased_prods as $refno => $qty){
					$this_cost=$cost;
					$this_bv=$total_bv;
					if(!mysql_query($sql="insert into $db->purchase_detail (slid, refno, title, prod_type, qty, amount, bv, dp, rp, total, total_bv) values ('$pid', '".AddSlashes($refno)."', '".AddSlashes($r_package_d[$refno])."', 'n', '$qty', '$cost', '$total_bv', '$total_dp', '$total_rp', '$this_cost', '$this_bv')")){
						$errmsg.="Error creating the sales detail. Please manually execute the SQL below to record the sales detail.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
					}
				}
			}
			//credit commission to upline, if this is the first sale
			if($first_sale){//1st sale
				$total_level=1;
				$level=1;
				$upline_id=$r_user['ref'];
				while($upline_id && $level<=$total_level){
					$r_upline=get_user_detail_by_id($upline_id);
					//upline only can receive comm if activated
					if($r_upline['activated']=='y'){
						$percent=$vars['product_ref_comm'];
						$comm_amount=round($total_bv * $percent / 100, 2);
						//create comm record
						if(!mysql_query($sql="insert into $db->member_comm (cid, uid, from_uid, type, amount, percent, level, year, month, day, status, cdate) values ('$pid', '$upline_id', '$post_d[uid]', 'r', '$comm_amount', '$percent', '$level', '".ndate('Y')."', '".ndate('n')."', '".ndate('j')."', 'confirmed', '$datetime')")){
							$errmsg.="Error creating the commission record for upline ID #$upline_id (level $level) for this sales. Please manually execute the SQL below to record the transaction:<br />\n<br />\n$sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
						}
						//credit to upline ewallet
						if(!mysql_query($sql="update $db->users set ewallet=ewallet+$comm_amount where id='$upline_id' limit 1")){
							$errmsg.="Error crediting $comm_amount of $vars[ewallet_title] to the member ID#$upline_id as sponsor commission. Please manually add back the amount to this member.<br /><br />$sql<br /><br />Error: ".mysql_error()."<br />";
						}elseif(!mysql_query($sql="insert into $db->member_ewallet_record (uid, type, amount, descr, cdate) values ('$upline_id', 'credit', '$comm_amount', '".addslashes("Sponsor Commission received from downline ID#$post_s[uid] activation ".($r_user['rank_a']>0? "upgrade from {$vars['rank_index_a_d'][$r_user['rank_a']]} to $r_package_d[$refno]" : "for $r_package_d[$refno]"))."', '".ndate()."')")){
							$errmsg.="Error creating the $vars[ewallet_title] record history for the member ID#$upline_id as sponsor commission record. Please manually record this transaction by executing SQL.<br /><br />$sql<br /><br />Error: ".mysql_error()."<br />";
						}else{
							$new_eid = mysql_insert_id();
							$last_bal = @mysql_result(mysql_query("select bal from $db->member_ewallet_record where uid='$upline_id' and id!=$new_eid order by id desc limit 1"), 0);
							if(!$last_bal){
								$last_bal = 0;
							}
							$last_bal += $comm_amount;
							@mysql_query("update $db->member_ewallet_record set bal='$last_bal' where id='$new_eid' limit 1");
						}
						//maintenance record
						$maintain_time=mktime(0, 0, 0, ndate('n'), 1, ndate('Y'));
						$maintain_year = date("Y", $maintain_time);
						$maintain_month = date("n", $maintain_time);
						$total_maintain_amount = $maintain_amount = round($comm_amount * $vars['maintenance_percent'] / 100, 2);
						$next_month_maintain = @mysql_result(mysql_query("select sum(amount) from $db->member_maintain where uid='$upline_id' and year='$maintain_year' and month='$maintain_month'"), 0) or 0;						$user_country = @mysql_result(mysql_query("select country from $db->users where id='$upline_id'"), 0) or 0;									if($user_country=='JP'){							$max_maintain = $vars['jp_max_maintain'];							$maintain_amount = $maintain_amount * $vars['jp_maintain_rate'];							$rate = $vars['jp_maintain_rate'];							$max_mall_maintain = $vars['jp_max_mall_maintain'];						}elseif($user_country=='TH'){							$max_maintain = $vars['th_max_maintain'];							$maintain_amount = $maintain_amount * $vars['th_maintain_rate'];							$rate = $vars['th_maintain_rate'];							$max_mall_maintain = $vars['th_max_mall_maintain'];						}elseif($user_country=='MY'){							$max_maintain = $vars['my_max_maintain'];							$maintain_amount = $maintain_amount * $vars['my_maintain_rate'];							$rate = $vars['my_maintain_rate'];							$max_mall_maintain = $vars['my_max_mall_maintain'];						}else{							$max_maintain = $vars['max_maintain'];							$maintain_amount = $maintain_amount * $vars['maintain_rate'];							$rate = $vars['maintain_rate'];							$max_mall_maintain = $vars['max_mall_maintain'];						}						
						if($next_month_maintain < $max_maintain){
							if($maintain_amount + $next_month_maintain > $max_maintain){
								$maintain_amount = $max_maintain - $next_month_maintain;
							}														if($user_country=='JP'){								$maintain_amount_ewallet = $maintain_amount / $vars['jp_maintain_rate'];							}elseif($user_country=='TH'){								$maintain_amount_ewallet = $maintain_amount / $vars['th_maintain_rate'];							}elseif($user_country=='MY'){								$maintain_amount_ewallet = $maintain_amount / $vars['my_maintain_rate'];							}else{								$maintain_amount_ewallet = $maintain_amount / $vars['maintain_rate'];							}
							if(!mysql_query($sql="update $db->users set ewallet=ewallet-$maintain_amount_ewallet, swallet=swallet+$maintain_amount where id='$upline_id' limit 1")){
								$errmsg.="Error deducting the maintenance amount of $maintain_amount_ewallet and adding the $vars[swallet_title] of $maintain_amount from the member ID#$upline_id as maintenance. Please manually deduct /add the amount to this member.<br /><br />$sql<br /><br />Error: ".mysql_error()."<br />";
							}else{
								if(!mysql_query($sql="insert into $db->member_ewallet_record (uid, type, amount, descr, cdate) values ('$upline_id', 'debit', '$maintain_amount_ewallet', '".addslashes("Debited ".number_format($maintain_amount_ewallet, 2)." for maintenance, deducted from Sponsor Commission received from downline ID#$post_s[uid] activation ".($r_user['rank_a']>0? "upgrade from {$vars['rank_index_a_d'][$r_user['rank_a']]} to $r_package_d[$refno]" : "for $r_package_d[$refno]"))."', '".ndate()."')")){
									$errmsg.="Error creating the $vars[ewallet_title] record history for the member ID#$upline_id as maintenance. Please manually record this transaction by executing SQL.<br /><br />$sql<br /><br />Error: ".mysql_error()."<br />";
								}else{
									$new_eid = mysql_insert_id();
									$last_bal = @mysql_result(mysql_query("select bal from $db->member_ewallet_record where uid='$upline_id' and id!=$new_eid order by id desc limit 1"), 0);
									if(!$last_bal){
										$last_bal = 0;
									}
									$last_bal -= $maintain_amount_ewallet;
									@mysql_query("update $db->member_ewallet_record set bal='$last_bal' where id='$new_eid' limit 1");
								}
								if(!mysql_query($sql="insert into $db->member_swallet_record (uid, type, amount, descr, cdate) values ('$upline_id', 'credit', '$maintain_amount', '".addslashes("Credited ".number_format($maintain_amount_ewallet, 2)." for maintenance, credited from Sponsor Commission received from downline ID#$post_s[uid] activation ".($r_user['rank_a']>0? "upgrade from {$vars['rank_index_a_d'][$r_user['rank_a']]} to $r_package_d[$refno]" : "for $r_package_d[$refno]"))."', '".ndate()."')")){
									$errmsg.="Error creating the $vars[swallet_title] record history for the member ID#$upline_id as maintenance. Please manually record this transaction by executing SQL.<br /><br />$sql<br /><br />Error: ".mysql_error()."<br />";
								}else{
									$new_eid = mysql_insert_id();
									$last_bal = @mysql_result(mysql_query("select bal from $db->member_swallet_record where uid='$upline_id' and id!=$new_eid order by id desc limit 1"), 0);
									if(!$last_bal){
										$last_bal = 0;
									}
									$last_bal += $maintain_amount;
									@mysql_query("update $db->member_swallet_record set bal='$last_bal' where id='$new_eid' limit 1");
								}
							}
							if(!mysql_query($sql="insert into $db->member_maintain (uid, comm_type, percent, credit, rate, amount, year, month, cdate) values ('$upline_id', 'r', '$vars[maintenance_percent]', '$maintain_amount_ewallet', '$rate', '$maintain_amount', '$maintain_year', '$maintain_month', '".ndate()."')")){
								$critical_error.="Error creating the maintenance record for the member ID#$upline_id. Please manually create the maintenance record by executing SQL.<br /><br />$sql<br /><br />Error: ".mysql_error()."<br />";
							}
						}else{
							$maintain_amount = 0;							$maintain_amount_ewallet = 0;
						}
						//mall point						$mall_point = $total_maintain_amount - $maintain_amount_ewallet;																			if(($next_month_maintain+$maintain_amount-$max_maintain)<$max_mall_maintain){							$max_mall_maintain = $max_mall_maintain - ($next_month_maintain+$maintain_amount-$max_maintain);							if($mall_point>$max_mall_maintain)								$mall_point = $max_mall_maintain;						}else{							$mall_point = 0;						}						
						if($mall_point > 0){
							if(!mysql_query($sql="update $db->users set ewallet=ewallet-$mall_point, mwallet=mwallet+$mall_point where id='$upline_id' limit 1")){
								$critical_error.="Error deducting the maintenance amount of $mall_point ($vars[mwallet_title]) and adding the $vars[mwallet_title] of $mall_point from the member ID#$upline_id. Please manually deduct /add the amount to this member.\n\n$sql\n\nError: ".mysql_error()."\n";
							}else{
								if(!mysql_query($sql="insert into $db->member_ewallet_record (uid, type, amount, descr, cdate) values ('$upline_id', 'debit', '$mall_point', '".addslashes("Debited ".number_format($mall_point, 2)." for maintenance, deducted from Sponsor Commission received from downline ID#$post_s[uid] activation ".($r_user['rank_a']>0? "upgrade from {$vars['rank_index_a_d'][$r_user['rank_a']]} to $r_package_d[$refno]" : "for $r_package_d[$refno]"))."', '".ndate()."')")){
									$critical_error.="Error creating the $vars[ewallet_title] record history for the member ID#$upline_id. Please manually record this transaction by executing SQL.\n\n$sql\n\nError: ".mysql_error()."\n";
								}else{
									$new_eid = mysql_insert_id();
									$last_bal = @mysql_result(mysql_query("select bal from $db->member_ewallet_record where uid='$upline_id' and id!=$new_eid order by id desc limit 1"), 0);
									if(!$last_bal){
										$last_bal = 0;
									}
									$last_bal -= $mall_point;
									@mysql_query("update $db->member_ewallet_record set bal='$last_bal' where id='$new_eid' limit 1");
								}
								if(!mysql_query($sql="insert into $db->member_mwallet_record (uid, type, amount, descr, cdate) values ('$upline_id', 'credit', '$mall_point', '".addslashes("Credited ".number_format($mall_point, 2)." for maintenance, credited from Sponsor Commission received from downline ID#$post_s[uid] activation ".($r_user['rank_a']>0? "upgrade from {$vars['rank_index_a_d'][$r_user['rank_a']]} to $r_package_d[$refno]" : "for $r_package_d[$refno]"))."', '".ndate()."')")){
									$critical_error.="Error creating the $vars[mwallet_title] record history for the member ID#$upline_id. Please manually record this transaction by executing SQL.\n\n$sql\n\nError: ".mysql_error()."\n";
								}else{
									$new_eid = mysql_insert_id();
									$last_bal = @mysql_result(mysql_query("select bal from $db->member_mwallet_record where uid='$upline_id' and id!=$new_eid order by id desc limit 1"), 0);
									if(!$last_bal){
										$last_bal = 0;
									}
									$last_bal += $mall_point;
									@mysql_query("update $db->member_mwallet_record set bal='$last_bal' where id='$new_eid' limit 1");
								}
							}
							if(!mysql_query($sql="insert into $db->member_maintain (uid, type, comm_type, percent, credit, rate, amount, year, month, cdate) values ('$upline_id', 'm', 'r', '$vars[maintenance_percent]', '$mall_point', '1', '$mall_point', '$maintain_year', '$maintain_month', '".ndate()."')")){
								$critical_error.="Error creating the maintenance record ($vars[mwallet_title]) for the member ID#$upline_id. Please manually create the maintenance record by executing SQL.\n\n$sql\n\nError: ".mysql_error()."\n";
							}
						}
					}
					if($r_upline['ref']){
						$upline_id=$r_upline['ref'];
					}else{
						unset($upline_id);
					}
					$level++;
				}
			}
		}

		if(!$errmsg){
			$msg=replace_tag(__("Sales successfully made for member ID '$post_s[uid]' ($r_user[name]) for the product '<%product%>'."), array("<%product%>"=>$purchase_descr))."<br />\n";
		}
	}

	$msg=$msg? format_msg($msg) : "";
	$errmsg=$errmsg? format_err(__("There is some error(s), please correct them before continuing:")."<br />\n<br />\n$errmsg") : "";
}

$form_fields=array("uid"=>"","payment_channel"=>"ewallet","package"=>"","status"=>"","_remark"=>"");
foreach($form_fields as $field => $default){
	$db_fieldname=preg_match('/^_/', $field)? substr($field, 1) : $field;
	$dis[$field]=!$post_s["__req"]? $default : $post_h[$field];
}
$payment_channel_select=build_select($r_payment_channel, $r_payment_channel_d, $dis['payment_channel'], 'payment_channel', $inputbox_style);
$package_select=build_select($r_package, $r_package_d, $dis['package'], 'package', $inputbox_style);


//javascript
foreach($vars['package'] as $refno=>$details){
 $prod_price_js.="prod_price['".AddSlashes($refno)."'] = $details[price];\n";
 $prod_bv_js.="prod_bv['".AddSlashes($refno)."'] = $details[bv];\n";
}
ob_start();
?>
<script type='text/javascript' src='<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js,jquery.livequery.js"); ?>'></script>
<script type='text/javascript'>
jQuery(document).ready(function(j){
	var prod_price = new Array();
	<?php echo $prod_price_js; ?>
	var prod_bv = new Array();
	<?php echo $prod_bv_js; ?>

	j('form[@name=activate_form]').submit(function(){
		var refno = j('select[@name=package] > option:selected').val();
		var pack = j('select[@name=package] > option:selected').text();
		var total_price = prod_price[refno];
		var total_bv = prod_bv[refno];
		if(confirm('<?php echo AddSlashes(__("Activate the member account by purchasing")); ?>:\n\n'+pack+'\n<?php echo AddSlashes(__("Total Price")); ?>: '+total_price+'\n<?php echo AddSlashes(__("Total BV")); ?>: '+total_bv+'\n\n<?php echo AddSlashes(__("Are you sure?")); ?>')){
			j('input[@name=submit_btn]').attr('disabled','disabled');
		}else{
			return false;
		}
	});

	j('input#check_user_btn').click(function(){
		j('span#user_text').html('').removeClass('red');
		if(j('input[@name=uid]').val()==''){
			j('span#user_text').html('<?php echo AddSlashes(__("Please provide a Member ID!")); ?>').addClass('red');
		}else{
			j(this).attr('disabled','disabled');
			j.ajax({
				url: '<?php echo "$this_file?aj=1&check_uid=1"; ?>',
				data: {'uid': j('input[@name=uid]').val()},
				type: 'post',
				dataType: 'xml',
				error: function(){
					j('span#user_text').html('<?php echo AddSlashes(__("Error checking...")); ?>').addClass('red');
				},
				success: function(data){
					var mesg='';
					if(j('loggedout', data).text()=='loggedout'){
						mesg='<?php echo AddSlashes(__("You have been logged out.")); ?>';
					}else{
						j(data).find('msg').each(function(){
						mesg+=j(this).text()+' ';
						});
					}
					var status=j(data).find('status').text();
					j('span#user_text').html(mesg).addClass(status==1? 'bold' : 'red');
				},
				complete: function(){
					j('input#check_user_btn').attr('disabled','');
				}
			});
		}
	});
});
</script>
<?php
$jvscript = ob_get_clean();

//user text
if($dis['uid']){
	if(!$r_user=get_user_detail_by_id($dis['uid'])){
		$user_text="<span class='red'>Member ID '$dis[uid]' could not be found.</span>";
	}elseif($r_user['rank_a']>=max($vars['rank_index_a'])){
		$ajerrmsg="<span class='red'>Member ID '$dis[uid] had already activated the top ranking and cannot be upgraded anymore.</span>";
	}else{
		$user_text="<span class='bold'>Member ID: $dis[uid], Name: $r_user[name], Rank: ".($r_user['rank_a']>0? $vars['rank_index_a_d'][$r_user['rank_a']] : "None").", $vars[ewallet_title]: ".number_format($r_user['ewallet'], 2)."</span>";
	}
}

//format input row
$display_fields=array("_remark");
foreach($form_fields as $field => $default){
	if(in_array($field, $display_fields)){
	$db_fieldname=preg_match('/^_/', $field)? substr($field, 1) : $field;
	$dbr=explode("#", $vars["dbr"]["purchase"][$db_fieldname]);
	$readonly=$textarea=false;
	$extra_display='';
	if(in_array($field, array("_remark"))){
	$textarea=true;
	}
	$readonly_str=$readonly? "readonly='readonly'" : "";
	$form_inputfield[$db_fieldname]="
	<tr>
		<td width='$td_width'>".__($dbr[4]).__(":").($dbr[3]=='m'? " ".__("*") : "")."</td>
		<td>".($textarea? "<textarea name='$field' rows='6' $inputbox_style $readonly_str>".$dis[$field]."</textarea>" : "
		<input type='text' name='$field' $inputbox_style value=\"".$dis[$field]."\" $readonly_str />")."$extra_display</td>
	</tr>";
	}
}

//activate form
$activate=($errmsg || $msg? $errmsg.$msg : "").
"<p>".__("You can activate the member account here.")."</p>
<form name='activate_form' method='post' action='$this_file'>
<input type='hidden' name='__req' value='1' />
<table class='amt_table'>
	<tr>
		<td width='$td_width'>".__("Member ID").__(":")." ".__("*")."</td>
		<td><input type='text' name='uid' value=\"".$dis['uid']."\" $inputbox_style /><span id='user_text'>$user_text</span> <input type='button' id='check_user_btn' value='Check Member' /></td>
	</tr>
	<tr>
		<td width='$td_width'>".__("Activation Package").__(":")." ".__("*")."</td>
		<td>$package_select</td>
	</tr>
	<tr>
		<td width='$td_width'>".__("Payment Channel").__(":")." ".__("*")."</td>
		<td>$payment_channel_select</td>
	</tr>
	$form_inputfield[remark]
	<tr>
		<td colspan='2' class='center' style='padding:20px 0 20px 0;'>
		<input type='submit' name='submit_btn' value=\"".__("Submit")."\" />
		</td>
	</tr>
</table>
</form>";

$content="<h2>$page_title</h2>".$activate;
print format_admin_page($content, $this_title, $jvscript);
?>